home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 019a / trim101.zip / TRIM11.C next >
C/C++ Source or Header  |  1991-11-06  |  6KB  |  158 lines

  1. /**********   trim.c    *****  TurboC v1   nov91
  2.  *  use  f:\>  trim   [path]filespec.ext  <cr>   (for ascii text files)
  3.  *  Pgm removes all trail blanks and trailing tabs from each line.
  4.  *  Also replaces each internal tab with 3 blank spaces.   The original ascii
  5.  *  file is altered & written to a temp file, then deleted. The temp file is
  6.  *  renamed to original name.   Transparent to user.
  7.  *  ken shirazee, mississauga, on, canada   member Cda Remote Systems BBS
  8.  
  9.  *  v1.1 nov 06/91    Sorry, v1.0 of nov 05/91 had bug.
  10.  
  11.  ***** */
  12.  
  13. #include  <stdio.h>
  14. #include  <string.h>
  15. #define   toascii(c)       ((c) & 0x7f)
  16.  
  17. int      _Cdecl unlink(char *filename);
  18. char     *workchar;
  19. char     *blankchar = " ";
  20.  
  21. main ( int argc,  char *argv[] )
  22. {
  23.  int    position;      /* VARS for create newfspec */
  24.  int    dotfound = 0;         /* = 0 if no '.' found in argv[1] */
  25.  char   orifspec[65];     /* copy of argv[1] on cmd line, fnam.ext to trim */
  26.  char   tmpfspec[65];     /* part of fnam, without .ext */
  27.  char   newfspec[65];     /* becomes fnam.!T!  for temporary file to create */
  28.                           /*   and later to rename as orifspec */
  29.                        /* VARS for write trimmed and de-tabbed lines */
  30.  FILE   *fp1,             /* ptr to orifspec, asciifil.ext, to be read */
  31.         *fpout;           /* ptr to newfspec, to be written-to */
  32.  char   oneword[100],     /* string, line read from asciifil.ext */
  33.         newword[100],     /* string where each tab is replaced w 3 blanks */
  34.         outword[100];     /* line w no trail blanks, written to temp file */
  35.  int    x,   i,   destlen;
  36.  
  37.  if (argc < 2)            /* f:\> trim <cr> is error, no input filename */
  38.     {
  39.      fprintf(stderr,"\tuse:  c:\\> trim  [path\\]ASCIIfil.ext <cr>\n");
  40.      fprintf(stderr,"\t or   c:\\> trim  ? <cr> for info");
  41.      exit(-1) ;
  42.      }
  43.  
  44.  if (stricmp(argv[1],"?") == 0)       /* trim ? on cmd line */
  45.     {
  46.      fprintf(stderr,"  usage is:  c:\\> trim  [path\\]ASCIIfil.ext <cr>\n");
  47.      fprintf(stderr,"\twhere ASCIIfil.ext is a TEXT file to be 'trimmed'.\n");
  48.      fprintf(stderr,"  TRIM.com removes trailing blanks & tabs on lines from\
  49.  input file;\n");
  50.      fprintf(stderr,"  also converts each internal tab to 3 blanks.   Output\
  51.  is written to\n");
  52.      fprintf(stderr,"  a TEMPORARY file.   The input file is deleted;\n");
  53.      fprintf(stderr,"  then the temporary file is RENAMED, as original input\
  54.  name.\n");
  55.      exit(-1) ;
  56.      }
  57.  
  58.  fp1   = fopen(argv[1],   "r");       /* ptr to adr of input fname, to read */
  59.  if (fp1 == NULL )
  60.     {
  61.      fprintf(stderr,"\tuse:  c\\> trim  [path\\]ASCIIfil.ext <cr>\n");
  62.      fprintf(stderr,"\t\t< %s > NOT FOUND.\n", argv[1]);
  63.      fclose(fp1);
  64.      exit(-1) ;
  65.      }
  66.  
  67.  strcpy(orifspec,  *++argv);                 /* copy the argv to orifspec */
  68.  
  69.                      /* look for position of LAST '.' in orifspec, if any */
  70.  for ( x=0,i=0;   x <= strlen(orifspec);    x++,i++ )
  71.    {
  72.     if ( 46 == ( toascii(orifspec[x]) ) )    /* dec value of '.' */
  73.       {
  74.        position = x + 1;                     /* position of LAST '.' */
  75.        dotfound = dotfound + 1;              /* add 1 for any '.' found */
  76.        tmpfspec[i] = orifspec[x];
  77.        }
  78.     else
  79.        tmpfspec[i] = orifspec[x];      /* no '.' in orifspec, ie argv[1] */
  80.     }
  81.  
  82.  if (dotfound == 1 )            /* only one '.' found in path AND filename */
  83.    {
  84.     strncpy(newfspec,   tmpfspec,   position - 1);    /* so .ext excluded */
  85.     newfspec[position - 1] = '\0';  /* add nul terminator before add .!T! */
  86.     strcat(newfspec,".!T!");
  87.     }
  88.  
  89.  if (dotfound == 0 )                                  /* no '.' in argv[1] */
  90.    {
  91.     strcpy(newfspec,    tmpfspec);
  92.     newfspec[i] = '\0';             /* add nul terminator before add .!T! */
  93.     strcat(newfspec,".!T!");
  94.     }
  95.  
  96.  if (dotfound == 2 )                           /* two '.' found in argv[1] */
  97.    {
  98.     strncpy(newfspec,   tmpfspec,   position - 1);    /* so .ext excluded */
  99.     newfspec[position - 1] = '\0';  /* add nul terminator before add .!T! */
  100.     strcat(newfspec,".!T!");
  101.     }
  102.  
  103.  fpout = fopen(newfspec,"w");
  104.  if (fpout == NULL )
  105.    {
  106.     printf("\nDebug 4, can't open temporary file, < %s > to write to\n",newfspec);
  107.     printf("\ncan't open temporary file, < %s > to write to\n",fpout);
  108.     exit(0);
  109.     }
  110.  
  111.   /* BIG LOOP *
  112.    * get 1 line at-a-time from user entered name input file, ends in 0A/0D.
  113.    * Reverse 'oneword' to locate position of 1st non-blank/non-tab.
  114.    */
  115.  while  ( fgets(oneword,100,fp1)  != NULL)
  116.    {
  117.     strrev(oneword);
  118.  
  119.     for (x=1;  x <= strlen(oneword) ;  x++ )   /* nb x=1 */
  120.       {
  121.        *workchar = oneword[x];
  122.        if ((stricmp(workchar, " ") != 0 )      /* if a non-space AND a */
  123.          && (stricmp(workchar, "    ") != 0 )) /* non-tab (ctrl I in quote) */
  124.            goto LABEL1;
  125.        }
  126.  
  127.     LABEL1:
  128.     strrev(oneword);      /* oneword reversed back to orig, [x] =last char */
  129.     destlen = strlen(oneword)  - x -1 ;
  130.  
  131.          /* ROUTINE TO replace 1 TAB (decimal 9) with 3 blanks in 'oneword',
  132.           * copy each char to make new string 'newword' */
  133.     for ( x=0, i=0;    x <= destlen ;    x++, i++ )
  134.      {
  135.       if ( 9 == ( toascii(oneword[x]) ) )
  136.         {
  137.          newword[i]   = *blankchar;
  138.          newword[i+1] = *blankchar;
  139.          newword[i+2] = *blankchar;
  140.          i = i + 2;                   /* when 3 blanks per tab */
  141.          }
  142.       else
  143.          newword[i] = oneword[x];  
  144.       }
  145.  
  146.     strncpy(outword, newword, i );
  147.     outword[i] = '\n' ;           /* add 0A/0D */
  148.     outword[i + 1] = '\0' ;       /* add nul terminator */
  149.     fprintf(fpout,  outword);     /* write to temporary file */
  150.     }      /* endo while */
  151.  
  152.  fclose(fp1);                     /* close input file */
  153.  fclose(fpout);                   /* close temporary file */
  154.  
  155.  unlink(orifspec);                /* DELETE input filename given on cmd line */
  156.  rename(newfspec, orifspec);      /* RENAME temp file to orig input filename */
  157.  }
  158.